package jfae.core.properties.parsers;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import jfae.core.properties.FieldInfo;


public class BuilderUtils {
	
	public static void assignPosition(List<? extends FieldInfo<?>> result) {
		int position = 0;
		for (FieldInfo<?> fieldInfo : result) {
			fieldInfo.setPosition(position++);
		}
		
	}
	
	public static void reorderFields(List<? extends FieldInfo<?>> result) {
		Collections.sort(result, new FieldsComparator());
		int order = 0;
		for (FieldInfo<?> fieldInfo : result) {
			fieldInfo.setOrder(order++);
		}
	}

	public static class FieldsComparator implements Comparator<FieldInfo<?>> {
		public int compare(FieldInfo<?> field1, FieldInfo<?> field2) {
			if(field1.getOrder() == field2.getOrder()){
				return 0;
			}else if(field1.getOrder() > field2.getOrder()){
				return 1;
			}
			return -1;
		}
	}
	
}
